interface FingerprintPageData{
  enable:boolean,
  pinAvaliable:boolean,
  deviceAvaliable:boolean,
  deviceSetup:boolean
}

interface FingerprintPageMethods{
  scanIfaa():void,
  verifyFingerPrint():void,
  verifyFaceId():void,
  checkEnable():void,
  checkFaceId():void,
  checkPin():void,
  checkDevice():void,
  checkDeviceSetup():void,
  enableFingerprint():void,
  disableFingerprint():void
}

Page<FingerprintPageData,FingerprintPageMethods>({
  data: {
    enable:false,
    pinAvaliable:false,
    deviceAvaliable:false,
    deviceSetup:false
  },
  onLoad() {
    this.checkEnable();
    this.checkPin();
    this.checkDevice();
    this.checkDeviceSetup();
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    //
  },
  checkDevice(){
    if (my.canIUse('checkIsSupportIfaaAuthentication')){
      my.checkIsSupportIfaaAuthentication({
        success: (res) => {
          if (res.supportMode.findIndex(f=>f === 'fingerPrint')>=0) {
            this.setData({deviceAvaliable:true});
          } else{
            this.setData({deviceAvaliable:false});
          }
        },
        fail: (error) => {
          this.setData({deviceAvaliable:false});
        }
      });
    } else {
      this.setData({ deviceAvaliable:false});
    }
  },
  checkDeviceSetup(){
    if (my.canIUse('checkIsIfaaEnrolledInDevice')){
      my.checkIsIfaaEnrolledInDevice({
        checkAuthMode:'fingerPrint',
        success: (res) => {
            this.setData({
              deviceSetup:res.isEnrolled
            });
        },
        fail:(res)=>{
          this.setData({
            deviceSetup:false
          })
        }
      });
    } else {
      this.setData({
        deviceSetup:false
      })
    }
  },
  checkEnable(){
    //TODO:获取当前小程序是否已设置指纹？？
    this.setData({enable:false});
  },
  checkPin(){
    //TODO:获取当前小程序是否已设置PIN？？
    this.setData({pinAvaliable:true});
  },
  enableFingerprint(){
    my.navigateTo({
      url:'/pages/setting/pininput'
    });
    this.setData({ enable:true});
  },
  disableFingerprint(){
    my.navigateTo({
      url:'/pages/setting/pininput'
    });
    this.setData({ enable:false});
  },
  scanIfaa(){
    //TODO:提取为指纹识别类，为后续做准备
    if (my.canIUse('startIfaaAuthentication')){
      my.checkIsSupportIfaaAuthentication({
        success: (res) => {
          if (res.supportMode.findIndex(f=>f === 'fingerPrint')>=0) {
            this.verifyFingerPrint();
          }  else if (res.supportMode.findIndex(f=>f === 'facial')>=0) {
            this.verifyFaceId();
          } else {
            console.log(JSON.stringify(res));
            my.alert({content:'抱歉，您的机型暂不支持指纹或者人脸识别功能'});
          }
        },
        fail: (res) => {
          console.log(JSON.stringify(res));
          my.alert({content:'抱歉，您的机型暂不支持指纹或者人脸识别功能'});
        }
      });
    }
  },
  verifyFaceId(){
      my.startIfaaAuthentication({
        requestAuthModes:['facial'],
        challenge:'123456',
        success:(res)=>{
          my.showToast({type:'success', content:'人脸验证成功！'});
        },
        fail:(res)=>{
          my.showToast({type:'fail',content:'人脸验证失败！'});
        }
      });
  },
  verifyFingerPrint():void{
    my.startIfaaAuthentication({
      requestAuthModes:['fingerPrint'],
      challenge:'123456',
      success:(res)=>{
        my.showToast({type:'success', content:'指纹验证成功！'});
      },
      fail:(res)=>{
        my.showToast({type:'fail',content:'指纹验证失败！'});
      }
    });
  },
});
